﻿using System;

namespace Aeon.Framework
{

/// <summary>
/// Value object for multicolored colors.
/// After accessing RGBA, RGBA will change, iterating
/// circularly over the colors supplied in the constructor.
/// 
/// </summary>
public struct AeonColorMany : IColor
    {
    // --- Attributes:

    private IColor[] colors;    // Colors list.

    private int partial;        // Circular component access counter.
    private int head;           // Circular variable current color.
    private int tail;           // End of colors list.

    // --- Properties:

    public float R { get { next(); return(colors[head].R); } set { colors[head].R = value; } }
    public float G { get { next(); return(colors[head].G); } set { colors[head].R = value; } }
    public float B { get { next(); return(colors[head].B); } set { colors[head].R = value; } }
    public float A { get { next(); return(colors[head].A); } set { colors[head].R = value; } }

    // --- Constructor:

    public AeonColorMany(params IColor[] many)
        {
        int count = many.Length;

        colors = new IColor[count];
        tail = count-1;
        partial = 0;
        head = 0;

        for (int cnt = 0; cnt < count; cnt++) { colors[cnt] = many[cnt]; }
        }

    // --- Private Methods:

    private void next()
        {
        partial++;
        
        if (partial == 4) { partial = 0; head++; }
        if (head > tail) head = 0;
        }

    // --- Comparison:

    private bool compare(IColor obj)
        {
        bool equals;

        if (colors[head].R == obj.R && colors[head].G == obj.G && colors[head].B == obj.B && colors[head].A == obj.A) equals = true;
        else equals = false;

        return (equals);
        }

    public override int GetHashCode()
        {

        return(colors[head].R.GetHashCode()^colors[head].G.GetHashCode()^colors[head].B.GetHashCode()^colors[head].A.GetHashCode());
        }

    public override bool Equals(Object obj)
        {
        bool equal = false;

        IColor c = obj as IColor;
        if (c != null) { equal = compare(c); }

        return(equal); 
        }

    public bool Equals(IColor c) 
        {
        bool equal = false;

        if (c is AeonColorMany && c != null) { equal = compare(c); }

        return(equal);
        }

    }
    
    
}